package com.socool;

import android.content.Context;
import android.location.Address;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SlideListFragment extends ListFragment {
	
	SampleAdapter adapter;
	
	final static int Your_Dates = 0;
	final static int Browse=1;
	final static int Add_Date=2;
	final static int My_team=3;
	final static int My_profile=4;
	final static int Setting=5;
	
	
	final String slide_Name[] = new String[]{"Your Dates","Browse","Add Date","My Friend","My Profile","Setting"};
	final int slide_icon[] = new int[]{R.drawable.slide_calender
										,R.drawable.slide_browse
										,R.drawable.slide_add
										,R.drawable.slide_team
										,R.drawable.slide_myprofile
										,R.drawable.slide_setting
										};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		adapter = new SampleAdapter(getActivity());
		for (int i = 0; i < slide_Name.length; i++) {
			//adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
			adapter.add(new SampleItem(slide_Name[i],  slide_icon[i]));
		}
		setListAdapter(adapter);
		
		
	}
	
	@Override
    public void onListItemClick(ListView parent, View v, int position, long id) { 
	
		Fragment newContent = null;
		String title="";
		switch(position){
		case Your_Dates:
			newContent = new Frag_YourDates();
			title = "Your Dates";
			break;
		case Browse:
			newContent = new Frag_Browse();
			title = "Browse";
			break;
		case Add_Date:
			newContent = new Frag_AddDate();
			title = "Add Date";
			break;
			
		case My_team:
			newContent = new Frag_MyFriend();
			title="My Friend";
			break;
			
		case My_profile:
			newContent = new Frag_Myprofile();
			title="My profile";
			break;
			
		case Setting:
			newContent = new Frag_Setting();
			title="Setting";
			break;
		}
		
		if (newContent != null)
			switchFragment(newContent,title);
    }
	

	// the meat of switching the above fragment
	private void switchFragment(Fragment fragment, String title) {
		if (getActivity() == null)
			return;
		
			HomeActivity fca = (HomeActivity) getActivity();
			fca.replaceFragment(fragment,title);
		
	}

	

	private class SampleItem {
		public String tag;
		public int iconRes;
		public SampleItem(String tag, int iconRes) {
			this.tag = tag; 
			this.iconRes = iconRes;
		}
	}

	public class SampleAdapter extends ArrayAdapter<SampleItem> {

		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_slidemenu, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			icon.setImageResource(getItem(position).iconRes);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);

			
			//Toast.makeText(getContext()	, "position: "+position	, Toast.LENGTH_SHORT).show();
			
			return convertView;
			
			
		}

	}
	
	
}
